/* eslint-disable no-console */
/* eslint-disable no-use-before-define */
const fs = require('fs');
const path = require('path');

const hook = process.argv[2];
console.log('start to run', hook);
console.log('current platform:', process.platform);

if (hook === 'postinstall') {
  if (process.platform === 'win32') {
    copyFolderSync('resources/lib/win', 'node_modules');
  } else {
    copyFolderSync('resources/lib/centos', 'node_modules');
  }
}

// 递归复制
function copyFolderSync(fromDir, destDIr) {
  if (!fs.existsSync(destDIr)) {
    fs.mkdirSync(destDIr);
  }
  fs.readdirSync(fromDir).forEach((element) => {
    if (fs.lstatSync(path.join(fromDir, element)).isFile()) {
      const fromPath = path.join(fromDir, element);
      const toPath = path.join(destDIr, element);
      console.log(`copy file: from ${fromPath} to ${toPath}`);
      fs.copyFileSync(fromPath, toPath);
    } else {
      copyFolderSync(path.join(fromDir, element), path.join(destDIr, element));
    }
  });
}
